

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.entity.GraveEntity;
import com.yz.dao.GraveDao;
import com.yz.dao.PotteryDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("graveService")
public class GraveServiceImpl extends ServiceImpl<GraveDao,GraveEntity> implements GraveService {
    @Autowired
    CheckService checkService;
    @Autowired
    DrawingService drawingService;
    @Autowired
    CameraService cameraService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postMe(GraveForm data, UserEntity user) {
        GraveEntity t = data.getGraveEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(t.getUpdateAt());
        check.setCreateUser(t.getCreateUser());
        check.setName(projectService.selectById(t.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[11]);
        check.setProjectId(t.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(108);
        long id = 0;
        if(t.getId()==0){
            this.baseMapper.insert(t);
            id = this.baseMapper.selectId();
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[11]+id);
            check.setDataId(id);
            checkService.insert(check);
        }else{
            id = t.getId();
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[11]+id);
            check.setDataId(id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[11]+id));
            this.baseMapper.update(t,new EntityWrapper<GraveEntity>().eq("id",id));
        }
        //图片放入数据库
        if(data.getDrawingNumber()!=null&& data.getDrawingNumber().size()>0){
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",108));
            for (int i = 0; i <data.getDrawingNumber().size() ; i++) {
                DrawingEntity item = data.getDrawingNumber().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setTypeForm(108);
                item.setDataId(id);
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[11]+id);
                drawingService.insert(item);
            }
        }
        if(data.getCameraNumber()!=null&& data.getCameraNumber().size()>0){
            cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",108));
            for (int i = 0; i <data.getCameraNumber().size(); i++) {
                CameraEntity item = data.getCameraNumber().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(t.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(t.getProjectId());
                item.setTypeWhere(2);
                item.setDataId(id);
                item.setTypeForm(108);
                item.setFiledName("0");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[11]+id);
                cameraService.insert(item);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteMe(long id) {
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",108));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",108));
        this.baseMapper.deleteById(id);
        checkService.deleteMe(5,108,id);
    }

    @Override
    public GraveForm getDetail(long id) {
        GraveForm data = new GraveForm();
        GraveEntity t = this.baseMapper.selectById(id);
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",108));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",108));
        data.setDrawingNumber(dds);
        data.setCameraNumber(cc1s);
        data.setGraveEntity(t);
        return data;
    }
}
